package com.jiedeng.controller;

import com.jiedeng.dao.*;
import com.jiedeng.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by qinyu on 2017/4/5.
 */
@RestController
@RequestMapping("/basic")
public class BasicController {

    @Autowired
    IRankRepository rankRepository;

    @Autowired
    INormTypeRepository normRepository;

    @Autowired
    IPropRepository propRepository;

    @Autowired
    INatRepository natRepository;

    @Autowired
    IProTypeRepository proTypeRepository;

    @Autowired
    IProDegreeRepository proDegreeRepository;

    @Autowired
    ISpecRepository specRepository;

    @Autowired
    ICorpPropRepository corpPropRepository;

    @Autowired
    ICorpRepository corpRepository;

    @Autowired
    IZoneRepository zoneRepository;

    @Autowired
    IGradeRepository gradeRepository;

    @Autowired
    IAskTypeRepository askTypeRepository;

    private  static String GLOBAL_CORP = "全局";

    //----------------- rank table -------------------//
    @RequestMapping(value = "/rank/all", method = RequestMethod.GET)
    public List<Rank> findAllRank(){
        return rankRepository.findAll();
    }

    @RequestMapping(value = "/rank/{id}", method = RequestMethod.GET)
    public Rank findRank(int id){
        return rankRepository.getOne(id);
    }

    @RequestMapping(value = "/rank/save", method = RequestMethod.POST)
    public Rank saveRank(@RequestBody Rank rank){
        return rankRepository.save(rank);
    }

    @RequestMapping(value = "/rank/delete", method = RequestMethod.POST)
    public void deleteRank(int id){
        rankRepository.deleteById(id);
    }

    //---------------- normal table  -----------------//
    @RequestMapping(value = "/norm/all", method = RequestMethod.GET)
    public List<NormalType> findAllNormal(){
        return normRepository.findAll();
    }

    @RequestMapping(value = "/norm/{id}", method = RequestMethod.GET)
    public NormalType getNormal(int id){
        return normRepository.getOne(id);
    }

    @RequestMapping(value = "/norm/save", method = RequestMethod.POST)
    public NormalType saveNormal(@RequestBody NormalType normalType){
        return normRepository.save(normalType);
    }

    @RequestMapping(value = "/norm/delete", method = RequestMethod.POST)
    public void deleteNormal(int id){
        normRepository.deleteById(id);
    }

    //----------------- grade table -------------------//
    @RequestMapping(value = "/grade/all", method = RequestMethod.GET)
    public List<Grade> findAllGrade(){
        return gradeRepository.findAll();
    }

    @RequestMapping(value = "/grade/{id}", method = RequestMethod.GET)
    public Grade findGrade(int id){
        return gradeRepository.getOne(id);
    }

    @RequestMapping(value = "/grade/save", method = RequestMethod.POST)
    public Grade saveGrade(@RequestBody Grade grade){
        return gradeRepository.save(grade);
    }

    @RequestMapping(value = "/grade/delete", method = RequestMethod.POST)
    public void deleteGrade(int id){
        gradeRepository.deleteById(id);
    }

    //----------------- asktype table -------------------//
    @RequestMapping(value = "/asktype/all", method = RequestMethod.GET)
    public List<AskType> findAllAskType(){
        return askTypeRepository.findAll();
    }

    @RequestMapping(value = "/asktype/{id}", method = RequestMethod.GET)
    public AskType findAskType(int id){
        return askTypeRepository.getOne(id);
    }

    @RequestMapping(value = "/asktype/save", method = RequestMethod.POST)
    public AskType saveAskType(@RequestBody AskType ask){
        return askTypeRepository.save(ask);
    }

    @RequestMapping(value = "/asktype/delete", method = RequestMethod.POST)
    public void deleteAskType(int id){
        askTypeRepository.deleteById(id);
    }

    //---------------- prop table  -----------------//
    @RequestMapping(value = "/prop/all", method = RequestMethod.GET)
    public List<Prop> findAllProp(){
        List<Prop> props = propRepository.findAll();
        for(Prop prop : props){
            if(prop.getCorpId() == -1){
                prop.setCorpName(GLOBAL_CORP);
            }else {
                Corp corp = corpRepository.getOne(prop.getCorpId());
                if(corp != null){
                    prop.setCorpName(corp.getCaption());
                }
            }
        }
        return propRepository.findAll();
    }

    @RequestMapping(value = "/prop/save", method = RequestMethod.POST)
    public Prop saveProp(@RequestBody Prop prop){
        return propRepository.save(prop);
    }

    @RequestMapping(value = "/prop/delete", method = RequestMethod.POST)
    public void deleteProp(int id){
        propRepository.deleteById(id);
    }

    //---------------- nat table  -----------------//
    @RequestMapping(value = "/nat/all", method = RequestMethod.GET)
    public List<Nat> findAllNat(){
        List<Nat> nats = natRepository.findAll();
        for(Nat nat : nats){
            if(nat.getCorpId() == -1){
                nat.setCorpName(GLOBAL_CORP);
            }else{
                Corp corp = corpRepository.getOne(nat.getCorpId());
                if(corp != null){
                    nat.setCorpName(corp.getCaption());
                }
            }
        }

        return nats;
    }

    @RequestMapping(value = "/nat/save", method = RequestMethod.POST)
    public Nat saveNat(@RequestBody Nat nat){
        return natRepository.save(nat);
    }

    @RequestMapping(value = "/nat/delete", method = RequestMethod.POST)
    public void deleteNat(int id){
        natRepository.deleteById(id);
    }

    //---------------- protype table  -----------------//
    @RequestMapping(value = "/pro_type/all", method = RequestMethod.GET)
    public List<ProType> findAllProType(){
        List<ProType> proTypes = proTypeRepository.findAll();
        for(ProType p : proTypes){
            if(p.getCorpId() == -1){
                p.setCorpName(GLOBAL_CORP);
            }else{
                Corp corp = corpRepository.getOne(p.getCorpId());
                if(corp != null){
                    p.setCorpName(corp.getCaption());
                }
            }
        }
        return proTypeRepository.findAll();
    }

    @RequestMapping(value = "/pro_type/save", method = RequestMethod.POST)
    public ProType saveProType(@RequestBody ProType proType){
        return proTypeRepository.save(proType);
    }

    @RequestMapping(value = "/pro_type/delete", method = RequestMethod.POST)
    public void deleteProType(int id){
        proTypeRepository.deleteById(id);
    }

    //---------------- proDgree table  -----------------//
    @RequestMapping(value = "/pro_degree/all", method = RequestMethod.GET)
    public List<ProDegree> findAllProDegree(){
        List<ProDegree> degrees = proDegreeRepository.findAll();
        for (ProDegree degree: degrees) {
            if(degree.getIsProType() > 0){
                ProType proType = proTypeRepository.getOne(degree.getIsProType());
                if(proType != null){
                    degree.setProTypeCaption(proType.getCaption());
                }
            }
            if(degree.getCorpId() == -1){
                degree.setCorpName(GLOBAL_CORP);
            }else{
                Corp corp = corpRepository.getOne(degree.getCorpId());
                if(corp != null){
                    degree.setCorpName(corp.getCaption());
                }
            }
        }

        return degrees;
    }

    @RequestMapping(value = "/pro_degree/save", method = RequestMethod.POST)
    public ProDegree saveProType(@RequestBody ProDegree ProDegree){
        return proDegreeRepository.save(ProDegree);
    }

    @RequestMapping(value = "/pro_degree/delete", method = RequestMethod.POST)
    public void deleteProDegree(int id){
        proDegreeRepository.deleteById(id);
    }

    @RequestMapping(value = "/pro_degree/find_by_type", method = RequestMethod.GET)
    public List<ProDegree> findByProType(int proType){
        return  proDegreeRepository.findByIsProType(proType);
    }

    //---------------- spec table  -----------------//
    @RequestMapping(value = "/spec/all", method = RequestMethod.GET)
    public List<Spec> findAllSpec(){
        List<Spec> specs = specRepository.findAll();
        for(Spec spec : specs){
            if(spec.getCorpId() == -1){
                spec.setCorpName("全局");
            }else{
                Corp corp = corpRepository.getOne(spec.getCorpId());
                if(corp != null){
                    spec.setCorpName(corp.getCaption());
                }
            }
        }
        return specs;
    }

    @RequestMapping(value = "/spec/save", method = RequestMethod.POST)
    public Spec saveSpec(@RequestBody Spec Spec){
        return specRepository.save(Spec);
    }

    @RequestMapping(value = "/spec/delete", method = RequestMethod.POST)
    public void deleteSpec(int id){
        specRepository.deleteById(id);
    }


    //----------------- corp table -------------------//
    @RequestMapping(value = "/corp/all", method = RequestMethod.GET)
    public List<Corp> findAllCorp(){
        List<Corp> corps = corpRepository.findAll();
        for(Corp corp : corps){
            if(corp.getCorpPropId() > 0){
                CorpProp corpProp = corpPropRepository.getOne(corp.getCorpPropId());
                corp.setCorpPropCaption(corpProp.getCaption());
            }

            if(corp.isPower()){
                corp.setPowerStr("已授权");
            }else{
                corp.setPowerStr("未授权");
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if(corp.getTime() != null){
                String timeStr = sdf.format(corp.getTime());
                corp.setTimeStr(timeStr);
            }
            if(corp.getPowerTime() != null){
                String powerTimeStr = sdf.format(corp.getPowerTime());
                corp.setPowerTimeStr(powerTimeStr);
            }

            if(corp.getZoneId() > 0){
                Zone zone = zoneRepository.getOne(corp.getZoneId());
                if(zone != null){
                    corp.setZoneCaption(zone.getCaption());
                }
            }
            if(corp.getRelationShip() > 0){
                Corp corpRel = corpRepository.getOne(corp.getId());
                if(corpRel != null){
                    corp.setRelationShipCaption(corpRel.getCaption());
                }
            }
        }

        Corp globalCorp = new Corp();
        globalCorp.setId(-1);
        globalCorp.setCaption("全局");
        corps.add(globalCorp);

        return corps;
    }

    @RequestMapping(value = "/corp/{id}", method = RequestMethod.GET)
    public Corp findCorp(long id){
        return corpRepository.getOne(id);
    }

    @RequestMapping(value = "/corp/save", method = RequestMethod.POST)
    public Corp saveCorp(@RequestBody Corp corp){
        return corpRepository.save(corp);
    }

    @RequestMapping(value = "/corp/delete", method = RequestMethod.POST)
    public void deleteCorp(long id){
        corpRepository.deleteById(id);
    }

    //----------------- corpProp table -------------------//
    @RequestMapping(value = "/corpProp/all", method = RequestMethod.GET)
    public List<CorpProp> findAllCorpProp(){
        return corpPropRepository.findAll();
    }

    @RequestMapping(value = "/corpProp/{id}", method = RequestMethod.GET)
    public CorpProp findCorpProp(long id){
        return corpPropRepository.getOne(id);
    }

    @RequestMapping(value = "/corpProp/save", method = RequestMethod.POST)
    public CorpProp saveCorpProp(@RequestBody CorpProp corp){
        return corpPropRepository.save(corp);
    }

    @RequestMapping(value = "/corpProp/delete", method = RequestMethod.POST)
    public void deleteCorpProp(long id){
        corpPropRepository.deleteById(id);
    }



    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result saveRank1(@RequestBody User rank, HttpServletRequest request){
        Result rst = new Result();
        rst.setResultCode(1);
        rst.setMessage("OK");
        rst.setAuth("auth code");

        return rst;
    }
}

class Result {
    private int ResultCode;
    private String Message;
    private String Auth;

    public int getResultCode() {
        return ResultCode;
    }

    public void setResultCode(int resultCode) {
        ResultCode = resultCode;
    }

    public String getMessage() {
        return Message;
    }

    public void setMessage(String message) {
        Message = message;
    }

    public String getAuth() {
        return Auth;
    }

    public void setAuth(String auth) {
        Auth = auth;
    }
}

class User{

    private String UserName;
    private String Password;

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }
}